﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PrjSerails.ClientWork
{
    public class AutoWork
    {
        public DALControl MJJ = null;
        public SS_SocketServer.OperProcess operprocess = null;
        Thread mjjThread = null;
        int fixId = 1;

        //测试数据库
        private bool TestDb()
        {
            Model.BasicInfo bj = new Db.BLL.BasicInfo().GetModel();
            if (bj != null)
                return true;
            else
            {
                DiskTool.copyFile(Application.StartupPath + @"\db\bak", Application.StartupPath + @"\db\mjj");
                Model.BasicInfo bj2 = new Db.BLL.BasicInfo().GetModel();
                if (bj2 != null)
                    return true;
            }
            return false;

        }

        /// <summary>
        /// 密集架自动工作
        /// </summary>
        private void StartMJJ()
        {
            bool r = false;
            Model.BasicInfo bInfo = new Model.BasicInfo();
            bInfo = new Db.BLL.BasicInfo().GetModel();
            MJJ = new DALControl((int)bInfo.AreaId, (int)bInfo.ShelfNum);
            Global.ShelfNum = (int)bInfo.ShelfNum;
            Global.FixedID = (int)bInfo.ShelfFixedId;
            if (MJJ != null)
            {
                MJJ.InitCom();
                MJJ.SendArea();
            }
            r=MJJ.QueryMasterId(ref fixId);
            if (r == true)
            {
                fixId = MJJ.FixedId;
                Global.FixedID = fixId;
                MJJ.HumSet = (int)bInfo.HumSet;
                MJJ.TemSet = (int)bInfo.TemSet;
                MJJ.TemIntTime = (int)bInfo.TemIntTime;
                mjjThread = new Thread(MJJ.StartRunMjj);
                mjjThread.Start();
            }
            else
            {
                //this..Text = "密集架通迅失败.";
                MessageBox.Show("密集架通迅失败,请检测通迅线路!");
            }
        }

        public AutoWork()
        {
            if (TestDb() == false)
            {
                MessageBox.Show("数据损坏，请检查");
                return;
            }
            StartMJJ();
            operprocess = new SS_SocketServer.OperProcess();
            operprocess.StartSocketServer();
        }

        
    }
}
